﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Sql;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;

namespace CommonClasses
{
    public class Utility
    {


//------------------------------EMAIL ---------------------------------------------
        private string _emailto;
        private string _emailfrom;
        private string _password;
        private string _subject;
        private string _body;

        public string EMailTo { get { return _emailto; } set { this._emailto = value; } }
        public string EMailFrom { get { return _emailfrom; ; } set { this._emailfrom = value; } }
        public string Password { get { return _emailto; } set { this._emailto = value; } }
        public string Subject { get { return _subject; } set { this._subject = value; } }
        public string Body { get { return _body; } set { this._body = value; } }
        
        //sends email to and from typed in address
        // System.Net.Mail and System.Net reference needed (has built in check for email formatting)
        //should be able to send attachment, but haven't done that yet
        public string SendEmail(string emailTo, string emailFrom, string password, string subject, string body)
        {
            string results = "";
            //for sending email through gmail, will change for Charlotte Pipe?
            string smtpAddress = "smtp.gmail.com";
            int portNumber = 587;
            bool enableSSL = true;

            _emailto =emailTo;
            _emailfrom = emailFrom;
            _password =password;
            _subject = subject;
            _body = body;

            try
            {
                using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress(_emailfrom);
                    mail.To.Add(_emailto);
                    mail.Subject = _subject;
                    mail.Body = _body;
                    mail.IsBodyHtml = true;
                    // Can set to false, if you are sending pure text.

                    //mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
                    //mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));

                    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                    {
                        smtp.Credentials = new NetworkCredential(_emailfrom, _password);
                        smtp.EnableSsl = enableSSL;
                        smtp.Send(mail);
                    }
                }
                results = "Email sent.";
            }
            catch (Exception e)
            {
               results = "An error occurred: " + e;
            }
                return results;
  
        }//end sendEmail
       
//-------------------------Get Current Crew Methods (by plant or by date, time, plant) -------------------------------

        private string _plant;
        public string Plant { get { return _plant; } set { this._plant = value; } }
        private DateTime _date;
        public DateTime Date { get { return _date; } set { this._date = value; } }
        private string _time;
        public string Time { get { return _time; } set { this._time = value; } }
        //calculates what crew is working at the current time/date given the plant 
        public int WhatCrewWorkingNowbyPlant(string plant)
        {
            _plant = plant;
            DateTime startdate = DateTime.Parse("01-01-2000");
            int hour = DateTime.Now.Hour;
            DateTime date = DateTime.Now.Date;
            TimeSpan elapseddays = date.Subtract(startdate);
            double daysAgo = elapseddays.TotalDays;
            int shiftworking = 0;

            //get number of days in shift by plant
            double shiftdays = 3; //default plant days
            if (_plant == "NC Plant")
            { shiftdays = 4; }

            //get number of shift since start date.  Need to know if odd or even.
            int shiftnumber = Convert.ToInt32(daysAgo / shiftdays);

            //if the odd shift
            if (shiftnumber % 2 != 0)
            {   //between hours of 7am and 7pm
                if (hour >= 7 && hour < 19)
                { shiftworking = 1; }
                else
                { shiftworking = 3; }
            }
            else //if the even shift
            {
                if (hour >= 7 && hour < 19)
                { shiftworking = 2; }
                else
                { shiftworking = 4; }
            }
            return shiftworking;
        }// end  WhatCrewWorkingNowbyPlant

        

        //get what crew is working given date, time, and plant 
        //might be used for reports or future scheduling
        public string WhatCrewWorking(DateTime date, string time, string plant)
        {
            _plant = plant;
            _date = date;
            _time = time;
            string results = "";
            DateTime startdate = DateTime.Parse("01-01-2000");
            int hour = DateTime.Now.Hour;
            TimeSpan elapseddays = _date.Subtract(startdate);
            double daysAgo = elapseddays.TotalDays;
            int submittedhour = 0;
            int shiftworking = 0;

            //get number of days in shift by plant
            double shiftdays = 3; //default plant days
            if (_plant == "NC Plant")
            { shiftdays = 4; }

            //get time of shift
            if (_time == "7pm to 7am")
            { submittedhour = 20; }
            else if (_time == "7am to 7pm")
            { submittedhour = 8; }
            else
            { submittedhour = hour; }

            //get number of shift since start date.  Need to know if odd or even.
            int shiftnumber = Convert.ToInt32(daysAgo / shiftdays);

            //if the odd shift
            if (shiftnumber % 2 != 0)
            {   //between hours of 7am and 7pm
                if (submittedhour >= 7 && submittedhour < 19)
                { shiftworking = 1; }
                else
                { shiftworking = 3; }
            }
            else //if the even shift
            {
                if (submittedhour >= 7 && submittedhour < 19)
                { shiftworking = 2; }
                else
                { shiftworking = 4; }
            }
            return results = String.Format("The crew working  {0} on {1} in the {2} is crew {3}.", time, date.ToString("d"), plant, shiftworking);
        }// end WhatCrewWorking

//--------------------------Print (No longer being used by project) -------------
        //public static string Print(string printerName, string fileName)
        //{
        //    
        //}

        public static string GetConnectionString(string dbname)
        {
            string stringresult = "x";

            switch (dbname)
            {

                case "QCConfigurationManager":
                    stringresult = "Server=t-sql;DataBase=QConfigurationManager;Trusted_Connection=Yes;";
                    break;

                case "ConfigurationManager":
                    stringresult = "Server=t-sql;DataBase=ConfigurationManager;Trusted_Connection=Yes;";
                    break;

                case "ApplicationLog":
                    stringresult = "Server=t-sql;DataBase=ApplicationLog;Trusted_Connection=Yes;";
                    break;

                case "Order":
                    stringresult = "Server=t-sql;DataBase=Order;Trusted_Connection=Yes;";
                    break;

                case "Measurement":
                    stringresult = "Server=t-sql;DataBase=Measurement;Trusted_Connection=Yes;";
                    break;

                default:
                    stringresult = "Invalid Connection String";
                    break;

            }
            return stringresult;

        }
    }
}
